{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8dab3a49-3efc-40da-806c-b6e48d2ef34b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from PIL import Image\n",
    "\n",
    "path = 'pic.jpg'\n",
    "image = Image.open(path)\n",
    "img = np.array(image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c38d22d9-9b2f-4856-bc3e-3fce3c153ec5",
   "metadata": {},
   "outputs": [],
   "source": [
    "img.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a85e6e93-7b2a-42ae-9a46-4ba7353a4bec",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.imshow(img)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d4abddaa-7e9a-4297-a314-0de6250a409a",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.imshow(img[::-1, :, :])  # 上下翻转"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43afaa77-db43-4ff7-8ed2-aa1a6e46ebe8",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.imshow(img[::-2, ::-2, :])  # 每个维度的取值范围, 步进为负表示倒序取值, 步进越大, 裁剪越多, 采样越少 用于压缩图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "acecf193-6606-49f1-89a0-c9e4ca6e6d8d",
   "metadata": {},
   "outputs": [],
   "source": [
    "image.rotate(30)  # 旋转"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "734224f9-b115-41bc-8e21-cdad36743fb5",
   "metadata": {},
   "outputs": [],
   "source": [
    "img1 = np.clip(img * 1.2, a_min=0, a_max=255).astype('uint8') # 亮度增加到1.2倍, 即rgb值都乘以1.2, 再使用clip限制最大值(超过则替换)\n",
    "plt.imshow(img1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "05029189-894a-4e55-981b-eadb3f995bb5",
   "metadata": {},
   "outputs": [],
   "source": [
    "image.convert('L')  # 灰度\n",
    "# plt.imshow(img2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12cf7f57-40fa-48cc-a7b8-dbc9b1db9534",
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import ImageFilter\n",
    "\n",
    "image.filter(ImageFilter.GaussianBlur)  #高斯模糊 参考: https://www.cnblogs.com/invisible2/p/9177018.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ce58c19-035d-489a-8636-387145c17a2c",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.exp(1), np.pi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e1b4bc8-8f9b-49a0-bcf5-6f17eca542cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "image.filter(ImageFilter.BLUR) # 普通模糊"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "540b2b72-a5f9-497e-9239-42db17ded340",
   "metadata": {},
   "outputs": [],
   "source": [
    "image.filter(ImageFilter.EDGE_ENHANCE) # 边缘增强"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fe2e7fd2-7d4a-4a33-8160-7cb0905aea4b",
   "metadata": {},
   "outputs": [],
   "source": [
    "image.filter(ImageFilter.FIND_EDGES) # 找出边缘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "271de9b7-36d0-46b1-bc87-6872698aacfd",
   "metadata": {},
   "outputs": [],
   "source": [
    "image.filter(ImageFilter.EMBOSS) # 浮雕"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0e768b5b-7a03-4e5c-9114-88ada1140648",
   "metadata": {},
   "outputs": [],
   "source": [
    "image.filter(ImageFilter.CONTOUR) # 轮廓"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "282c7492-00f3-43e7-aa2e-9a62a36aef23",
   "metadata": {},
   "outputs": [],
   "source": [
    "image.filter(ImageFilter.SMOOTH) # 平滑"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7ce1d66c-03e6-4a89-8780-12914074888e",
   "metadata": {},
   "outputs": [],
   "source": [
    "image.filter(ImageFilter.SHARPEN) # 锐化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "02964aeb-acd4-4f02-8fe1-fae5e1e55074",
   "metadata": {},
   "outputs": [],
   "source": [
    "image.filter(ImageFilter.DETAIL) # 细节"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
